package com.ruoyi.web.controller.shop.cms;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.cms.domain.ShopCmsArticle;
import com.ruoyi.shop.cms.service.IShopCmsArticleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商城文章Controller
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/cms/article")
public class ShopCmsArticleController extends BaseController {
	private String prefix = "shop/cms/article";

	@Autowired
	private IShopCmsArticleService shopCmsArticleService;

	@RequiresPermissions("cms:article:view")
	@GetMapping()
	public String article() {
		return prefix + "/article";
	}

	/**
	 * 查询文章列表
	 */
	@RequiresPermissions("cms:article:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ShopCmsArticle shopCmsArticle) {
		startPage();
		List<ShopCmsArticle> list = shopCmsArticleService.selectArticleList(shopCmsArticle);
		return getDataTable(list);
	}

	/**
	 * 新增文章
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存文章
	 */
	@RequiresPermissions("cms:article:add")
	@Log(title = "文章", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ShopCmsArticle article) {
		return toAjax(shopCmsArticleService.insertArticle(article));
	}

	/**
	 * 修改文章
	 */
	@RequiresPermissions("cms:article:edit")
	@GetMapping("/edit/{articleId}")
	public String edit(@PathVariable("articleId") Long articleId, ModelMap mmap) {
		ShopCmsArticle article = shopCmsArticleService.selectArticleById(articleId);
		mmap.put("article", article);
		return prefix + "/edit";
	}

	/**
	 * 修改保存文章
	 */
	@RequiresPermissions("cms:article:edit")
	@Log(title = "文章", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ShopCmsArticle shopCmsArticle) {
		return toAjax(shopCmsArticleService.updateArticle(shopCmsArticle));
	}

	/**
	 * 删除文章
	 */
	@RequiresPermissions("cms:article:remove")
	@Log(title = "文章", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(shopCmsArticleService.deleteArticleByIds(ids));
	}
}
